/*
 *  Copyright (C) 2010 Matthias Buch-Kromann <mbk.isv@cbs.dk>
 * 
 *  This file is part of the MatrixParser package.
 *  
 *  The MatrixParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.osdtsystem.matrixparser.featureextraction;

import org.osdtsystem.matrixparser.data.CONLLSentence;
import org.osdtsystem.matrixparser.parsers.output.DependencyTree;
import org.osdtsystem.matrixparser.features.FeatureHandler;
import org.osdtsystem.matrixparser.features.FeatureVector;
import org.osdtsystem.matrixparser.parsers.ParsingModel;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public class HigherOrderMstSecondOrder extends AbstractHigherOrderExtractor {
    // Genes
    static final String name = "mst2";
    static final int siblingGeneCount = 12;
    static final int tripGeneCount = 2;
    static final int genes = siblingGeneCount + tripGeneCount;
    int gene = 0;

    public HigherOrderMstSecondOrder(ParsingModel model) {
        super(model, name, genes);
    }

    public void extractHigherOrder(FeatureVector fv, DependencyTree tree, int node) {
        CONLLSentence sentence = tree.sentence();

        // Right children
        int prevChild = node;
        for (int child : tree.rightChildren(node)) {
            gene = firstGene();
            addSiblingFeatures(sentence, prevChild, child, prevChild == node, fv);
            addTripFeatures(sentence, node, prevChild, child, fv);
            setLastGene(gene);
        }

        // Left children
        prevChild = node;
        for (int child : tree.leftChildren(node)) {
            gene = firstGene();
            addSiblingFeatures(sentence, prevChild, child, prevChild == node, fv);
            addTripFeatures(sentence, node, prevChild, child, fv);
            setLastGene(gene);
        }
    }

    void addSiblingFeatures(CONLLSentence sentence, int child1, int child2, boolean isStopWord, FeatureVector fv) {
        // child1 is always closer to the parent than child2
        String dir = child1 > child2 ? "RA" : "LA";

        String postag1 = isStopWord ? FeatureHandler.STARTSYMBOL : sentence.get(child1).postag();
        String word1 = isStopWord ? FeatureHandler.STARTSYMBOL : sentence.get(child1).form();
        String postag2 = sentence.get(child2).postag();
        String word2 = sentence.get(child2).form();

        // Sibling adjacency features
        if (on(gene++)) add(fv, "CH_PAIR", postag1, postag2, dir);
        if (on(gene++)) add(fv, "CH_WPAIR", word1, word2, dir);
        if (on(gene++)) add(fv, "CH_WPAIRA", word1, postag2, dir);
        if (on(gene++)) add(fv, "CH_WPAIRB", postag1, word2, dir);
        if (on(gene++)) add(fv, "ACH_PAIR", postag1, postag2);
        if (on(gene++)) add(fv, "ACH_WPAIR", word1, word2);
        if (on(gene++)) add(fv, "ACH_WPAIRA", word1, postag2);
        if (on(gene++)) add(fv, "ACH_WPAIRB", postag1, word2);

        // Sibling distance features
        int dist = Math.abs(child1 - child2);
        String distBool;
        if (dist > 10) {
            distBool = "10";
        } else if (dist > 5) {
            distBool = "5";
        } else {
            distBool = "" + (dist - 1);
        }

        if (on(gene++)) add(fv, "SIB_PAIR_DIST", distBool, dir);
        if (on(gene++)) add(fv, "ASIB_PAIR_DIST", distBool);
        if (on(gene++)) add(fv, "CH_PAIR_DIST=", postag1, postag2, distBool, dir);
        if (on(gene++)) add(fv, "ACH_PAIR_DIST=", postag1, postag2, distBool);
    }

    void addTripFeatures(CONLLSentence sentence,
            int parent,
            int child1, int child2,
            FeatureVector fv) {
        // ch1 is always the closest to par
        String dir = parent > child2 ? "RA" : "LA";

        String postagParent = sentence.get(parent).postag();

        String postag1 = child1 == parent ? FeatureHandler.STARTSYMBOL : sentence.get(child1).postag();
        String postag2 = sentence.get(child2).postag();

        if (on(gene++)) add(fv, "POS_TRIP", postagParent, postag1, postag2, dir);
        if (on(gene++)) add(fv, "APOS_TRIP", postagParent, postag1, postag2);
    }
}
